######################################################
#PA 2014 General Election Main Model.
# When Women Run, Voters Will Follow (Sometimes): 
##Examining the Mobilizing Effect of Female Candidates in the 2014 and 2018 Midterm Elections 
#By Safarpour, Wyckoff Gaynor, Rouse, and Swers #
######################################################

#clean the environment.
rm(list=ls()) 

#Packages.
library(mvtnorm) 
library(stargazer)
library(stats)
library(arm)
library(plyr)
library(Hmisc)
library(dplyr)
library(stargazer)

#setwd
setwd("/Users/ACS/Dropbox/When Women Run/Revision_PoliticalBehavior/R&R Part 2/Publication Docs/Replication Data and Code/")

#Step 1. Bring in fully recoded dataset.

PA<-read.csv("PA2014GeneralDataFinal.csv", 
             header = TRUE, stringsAsFactors = F)

#Step 2. Check Length.
length(PA$v1)

#Step 3. Run Model.
m1<- glm(voted2014general~ womangeneral+ 
           female2+ womangeneral*female2+ 
           Millennials2014+ womangeneral*Millennials2014+
           GenerationX2014+ womangeneral*GenerationX2014+
           SilentGeneration2014+ womangeneral*SilentGeneration2014+
           rep + womangeneral*rep+
           otherparty+ womangeneral*otherparty+
           voted2012+obamavote2012+totalreceipts,
         data=PA, family=binomial(link="logit"))
m1
summary(m1)



stargazer(m1, type="html", out="PA2014GeneralCompetitivenessControl.html",
          covariate.labels = c("Female Democratic Candidate", "Female",
                               "Millennials", "Generation X","Silent Generation", 
                               "Republican", "Other Party",
                               "Voted in 2012 General Election", 
                               "County Obama Vote 2012", 
                               "Total Money Raised",
                               "Female Democratic Candidate*Female",
                               "Female Democratic Candidate*Millennials",
                               "Female Democratic Candidate*Generation X",
                               "Female Democratic Candidate*Silent Generation",
                               "Female Democratic Candidate*Republican",
                               "Female Democratic Candidate*Other Party"),
          dep.var.labels="Voted in PA 2014 General Election", column.labels = "",
          title = "Effects of candidate gender, generation, gender,  party, prior voting, county Obama vote share, and total money raised on 2014 Midterm Turnout in PA",
          notes        = "Results from logistic regression. Standard errors in parentheses. Baseline age category: Baby Boomers. Baseline party: Democrats. Model excludes district 1, making the effect of female candidate only the effect for female Democratic candidates.", 
          notes.append = TRUE, notes.align = "l", digits=3, single.row = T)

#Step 4. Compute Predicted Probs.

#Compute Predicted Probabilities.
sample <- PA[complete.cases(PA$voted2014general, PA$womangeneral, PA$SilentGeneration2014, 
                            PA$Millennials2014, PA$GenerationX2014, PA$female2, PA$rep, PA$otherparty,
                            PA$voted2012, PA$obamavote2012, PA$totalreceipts)==T,] # Regression sample

preds <- predict(m1, type = "response")
options(digits=4)
summary(preds) #mean turnout.

#Overall effect for woman candidates.

n_draws <- 1000
set.seed(1714)
vcov<- vcov(m1)
coef<-coef(m1)
sim_coefs <- rmvnorm(n_draws, coef, vcov) #Specify the 1,000 simulated coefficients.
rbind(coef(m1), apply(sim_coefs, 2, mean)) # Check they are close to original


ppwomen0.s <- NULL
for (i in 1:n_draws) {
  ppwomen0.s[i] <- mean(invlogit(sim_coefs[i,1] + sim_coefs[i,2]*0 + 
                                   sim_coefs[i,3]*sample$female2 + 
                                   sim_coefs[i,4]*sample$Millennials2014 +
                                   sim_coefs[i,5]*sample$GenerationX2014 +
                                   sim_coefs[i,6]*sample$SilentGeneration2014+
                                   sim_coefs[i,7]*sample$rep+
                                   sim_coefs[i,8]*sample$otherparty+
                                   sim_coefs[i,9]*sample$voted2012+
                                   sim_coefs[i,10]*sample$obamavote2012+
                                   sim_coefs[i,11]*sample$totalreceipts+
                                   sim_coefs[i,12]*sample$female2*0+
                                   sim_coefs[i,13]*sample$Millennials2014*0 +
                                   sim_coefs[i,14]*sample$GenerationX2014*0 +
                                   sim_coefs[i,15]*sample$SilentGeneration2014*0+
                                   sim_coefs[i,16]*sample$rep*0+
                                   sim_coefs[i,17]*sample$otherparty*0))
}
mean(ppwomen0.s) #

ppwomen1.s <- NULL
for (i in 1:n_draws) {
  ppwomen1.s[i] <- mean(invlogit(sim_coefs[i,1] + sim_coefs[i,2]*1 + 
                                   sim_coefs[i,3]*sample$female2 + 
                                   sim_coefs[i,4]*sample$Millennials2014 +
                                   sim_coefs[i,5]*sample$GenerationX2014 +
                                   sim_coefs[i,6]*sample$SilentGeneration2014+
                                   sim_coefs[i,7]*sample$rep+
                                   sim_coefs[i,8]*sample$otherparty+
                                   sim_coefs[i,9]*sample$voted2012+
                                   sim_coefs[i,10]*sample$obamavote2012+
                                   sim_coefs[i,11]*sample$totalreceipts+
                                   sim_coefs[i,12]*sample$female2*1+
                                   sim_coefs[i,13]*sample$Millennials2014*1 +
                                   sim_coefs[i,14]*sample$GenerationX2014*1 +
                                   sim_coefs[i,15]*sample$SilentGeneration2014*1+
                                   sim_coefs[i,16]*sample$rep*1+
                                   sim_coefs[i,17]*sample$otherparty*1))
}
mean(ppwomen1.s) #


effect.01 <-  ppwomen1.s-ppwomen0.s
summary(effect.01) # (0-1 women candidate effect)

# CIs and put results together in matrix
elements <- list(ppwomen0.s, ppwomen1.s, effect.01) 
lapply(elements, summary) # See summary for each element of the list
ci <- do.call("rbind", (lapply(elements, quantile, c(.025,.975)))) # Get quntiles for each

results <- cbind(ci[,1], lapply(elements, mean), ci[,2])
colnames(results) <- c("2.5", "Mean", "97.5")   
rownames(results) <- c("No Women Ran", "Female Candidate", "Effect No Women-Female Candidate")
results

# Store mean effect and 95% CI to visualize results of m1 later.
meaneffect_m1<-results[[6]]
upperci_m1<-results[[3]]
lowerci_m1<-results[[9]]
##Get Predictions by Generation. 
w0.SG	<- NULL
w1.SG 	<- NULL
w0.BB	<- NULL
w1.BB 	<- NULL
w0.GX	<- NULL
w1.GX 	<- NULL
w0.M	<- NULL
w1.M 	<- NULL


SG <- subset(sample, SilentGeneration2014==1)
BB <- subset(sample, BabyBoomers2014==1)
GX <- subset(sample, GenerationX2014==1) 
M <- subset(sample, Millennials2014==1) 


for(i in 1:n_draws){ 
  w0.SG[i] <- mean(invlogit(sim_coefs[i,1] + sim_coefs[i,2]*0 + 
                              sim_coefs[i,3]*SG$female2 + 
                              sim_coefs[i,4]*SG$Millennials2014 +
                              sim_coefs[i,5]*SG$GenerationX2014 +
                              sim_coefs[i,6]*SG$SilentGeneration2014+
                              sim_coefs[i,7]*SG$rep+
                              sim_coefs[i,8]*SG$otherparty+
                              sim_coefs[i,9]*SG$voted2012+
                              sim_coefs[i,10]*SG$obamavote2012+
                              sim_coefs[i,11]*SG$totalreceipts+
                              sim_coefs[i,12]*SG$female2*0+
                              sim_coefs[i,13]*SG$Millennials2014*0 +
                              sim_coefs[i,14]*SG$GenerationX2014*0 +
                              sim_coefs[i,15]*SG$SilentGeneration2014*0+
                              sim_coefs[i,16]*SG$rep*0+
                              sim_coefs[i,17]*SG$otherparty*0))
  w1.SG[i] <- mean(invlogit(sim_coefs[i,1] + sim_coefs[i,2]*1 + 
                              sim_coefs[i,3]*SG$female2 + 
                              sim_coefs[i,4]*SG$Millennials2014 +
                              sim_coefs[i,5]*SG$GenerationX2014 +
                              sim_coefs[i,6]*SG$SilentGeneration2014+
                              sim_coefs[i,7]*SG$rep+
                              sim_coefs[i,8]*SG$otherparty+
                              sim_coefs[i,9]*SG$voted2012+
                              sim_coefs[i,10]*SG$obamavote2012+
                              sim_coefs[i,11]*SG$totalreceipts+
                              sim_coefs[i,12]*SG$female2*1+
                              sim_coefs[i,13]*SG$Millennials2014*1 +
                              sim_coefs[i,14]*SG$GenerationX2014*1 +
                              sim_coefs[i,15]*SG$SilentGeneration2014*1+
                              sim_coefs[i,16]*SG$rep*1+
                              sim_coefs[i,17]*SG$otherparty*1))
  w0.BB[i] <- mean(invlogit(sim_coefs[i,1] + sim_coefs[i,2]*0 + 
                              sim_coefs[i,3]*BB$female2 + 
                              sim_coefs[i,4]*BB$Millennials2014 +
                              sim_coefs[i,5]*BB$GenerationX2014 +
                              sim_coefs[i,6]*BB$SilentGeneration2014+
                              sim_coefs[i,7]*BB$rep+
                              sim_coefs[i,8]*BB$otherparty+
                              sim_coefs[i,9]*BB$voted2012+
                              sim_coefs[i,10]*BB$obamavote2012+
                              sim_coefs[i,11]*BB$totalreceipts+
                              sim_coefs[i,12]*BB$female2*0+
                              sim_coefs[i,13]*BB$Millennials2014*0 +
                              sim_coefs[i,14]*BB$GenerationX2014*0 +
                              sim_coefs[i,15]*BB$SilentGeneration2014*0+
                              sim_coefs[i,16]*BB$rep*0+
                              sim_coefs[i,17]*BB$otherparty*0))
  w1.BB[i] <- mean(invlogit(sim_coefs[i,1] + sim_coefs[i,2]*1 + 
                              sim_coefs[i,3]*BB$female2 + 
                              sim_coefs[i,4]*BB$Millennials2014 +
                              sim_coefs[i,5]*BB$GenerationX2014 +
                              sim_coefs[i,6]*BB$SilentGeneration2014+
                              sim_coefs[i,7]*BB$rep+
                              sim_coefs[i,8]*BB$otherparty+
                              sim_coefs[i,9]*BB$voted2012+
                              sim_coefs[i,10]*BB$obamavote2012+
                              sim_coefs[i,11]*BB$totalreceipts+
                              sim_coefs[i,12]*BB$female2*1+
                              sim_coefs[i,13]*BB$Millennials2014*1 +
                              sim_coefs[i,14]*BB$GenerationX2014*1 +
                              sim_coefs[i,15]*BB$SilentGeneration2014*1+
                              sim_coefs[i,16]*BB$rep*1+
                              sim_coefs[i,17]*BB$otherparty*1))
  w0.GX[i] <- mean(invlogit(sim_coefs[i,1] + sim_coefs[i,2]*0 + 
                              sim_coefs[i,3]*GX$female2 + 
                              sim_coefs[i,4]*GX$Millennials2014 +
                              sim_coefs[i,5]*GX$GenerationX2014 +
                              sim_coefs[i,6]*GX$SilentGeneration2014+
                              sim_coefs[i,7]*GX$rep+
                              sim_coefs[i,8]*GX$otherparty+
                              sim_coefs[i,9]*GX$voted2012+
                              sim_coefs[i,10]*GX$obamavote2012+
                              sim_coefs[i,11]*GX$totalreceipts+
                              sim_coefs[i,12]*GX$female2*0+
                              sim_coefs[i,13]*GX$Millennials2014*0 +
                              sim_coefs[i,14]*GX$GenerationX2014*0 +
                              sim_coefs[i,15]*GX$SilentGeneration2014*0+
                              sim_coefs[i,16]*GX$rep*0+
                              sim_coefs[i,17]*GX$otherparty*0))
  w1.GX[i] <- mean(invlogit(sim_coefs[i,1] + sim_coefs[i,2]*1 + 
                              sim_coefs[i,3]*GX$female2 + 
                              sim_coefs[i,4]*GX$Millennials2014 +
                              sim_coefs[i,5]*GX$GenerationX2014 +
                              sim_coefs[i,6]*GX$SilentGeneration2014+
                              sim_coefs[i,7]*GX$rep+
                              sim_coefs[i,8]*GX$otherparty+
                              sim_coefs[i,9]*GX$voted2012+
                              sim_coefs[i,10]*GX$obamavote2012+
                              sim_coefs[i,11]*GX$totalreceipts+
                              sim_coefs[i,12]*GX$female2*1+
                              sim_coefs[i,13]*GX$Millennials2014*1 +
                              sim_coefs[i,14]*GX$GenerationX2014*1 +
                              sim_coefs[i,15]*GX$SilentGeneration2014*1+
                              sim_coefs[i,16]*GX$rep*1+
                              sim_coefs[i,17]*GX$otherparty*1))
  w0.M[i] <- mean(invlogit(sim_coefs[i,1] + sim_coefs[i,2]*0 + 
                             sim_coefs[i,3]*M$female2 + 
                             sim_coefs[i,4]*M$Millennials2014 +
                             sim_coefs[i,5]*M$GenerationX2014 +
                             sim_coefs[i,6]*M$SilentGeneration2014+
                             sim_coefs[i,7]*M$rep+
                             sim_coefs[i,8]*M$otherparty+
                             sim_coefs[i,9]*M$voted2012+
                             sim_coefs[i,10]*M$obamavote2012+
                             sim_coefs[i,11]*M$totalreceipts+
                             sim_coefs[i,12]*M$female2*0+
                             sim_coefs[i,13]*M$Millennials2014*0 +
                             sim_coefs[i,14]*M$GenerationX2014*0 +
                             sim_coefs[i,15]*M$SilentGeneration2014*0+
                             sim_coefs[i,16]*M$rep*0+
                             sim_coefs[i,17]*M$otherparty*0))
  w1.M[i] <- mean(invlogit(sim_coefs[i,1] + sim_coefs[i,2]*1 + 
                             sim_coefs[i,3]*M$female2 + 
                             sim_coefs[i,4]*M$Millennials2014 +
                             sim_coefs[i,5]*M$GenerationX2014 +
                             sim_coefs[i,6]*M$SilentGeneration2014+
                             sim_coefs[i,7]*M$rep+
                             sim_coefs[i,8]*M$otherparty+
                             sim_coefs[i,9]*M$voted2012+
                             sim_coefs[i,10]*M$obamavote2012+
                             sim_coefs[i,11]*M$totalreceipts+
                             sim_coefs[i,12]*M$female2*1+
                             sim_coefs[i,13]*M$Millennials2014*1 +
                             sim_coefs[i,14]*M$GenerationX2014*1 +
                             sim_coefs[i,15]*M$SilentGeneration2014*1+
                             sim_coefs[i,16]*M$rep*1+
                             sim_coefs[i,17]*M$otherparty*1))}
effect.SG	<- w1.SG - w0.SG 
quantile(effect.SG, c(.025,.975)) #to get CIs; 
mean(effect.SG) #mean effect 

effect.BB	<-  w1.BB - w0.BB
quantile(effect.BB, c(.025,.975)) #to get CIs; 
mean(effect.BB) # mean effect 

effect.GX	<-  w1.GX - w0.GX
quantile(effect.GX, c(.025,.975)) #to get CIs; 
mean(effect.GX) # mean effect  

effect.M	<-  w1.M - w0.M
quantile(effect.M, c(.025,.975)) #to get CIs;
mean(effect.M) # mean effect 

#Effect by gender subgroup.
w0.Men	<- NULL
w1.Men 	<- NULL
w0.Women	<- NULL
w1.Women 	<- NULL

Men <- subset(sample, female2==0)
Women <- subset(sample, female2==1) 

for(i in 1:n_draws){ 
  w0.Men[i] <- mean(invlogit(sim_coefs[i,1] + sim_coefs[i,2]*0 + 
                               sim_coefs[i,3]*Men$female2 + 
                               sim_coefs[i,4]*Men$Millennials2014 +
                               sim_coefs[i,5]*Men$GenerationX2014 +
                               sim_coefs[i,6]*Men$SilentGeneration2014+
                               sim_coefs[i,7]*Men$rep+
                               sim_coefs[i,8]*Men$otherparty+
                               sim_coefs[i,9]*Men$voted2012+
                               sim_coefs[i,10]*Men$obamavote2012+
                               sim_coefs[i,11]*Men$totalreceipts+
                               sim_coefs[i,12]*Men$female2*0+
                               sim_coefs[i,13]*Men$Millennials2014*0 +
                               sim_coefs[i,14]*Men$GenerationX2014*0 +
                               sim_coefs[i,15]*Men$SilentGeneration2014*0+
                               sim_coefs[i,16]*Men$rep*0+
                               sim_coefs[i,17]*Men$otherparty*0))
  w1.Men[i] <- mean(invlogit(sim_coefs[i,1] + sim_coefs[i,2]*1 + 
                               sim_coefs[i,3]*Men$female2 + 
                               sim_coefs[i,4]*Men$Millennials2014 +
                               sim_coefs[i,5]*Men$GenerationX2014 +
                               sim_coefs[i,6]*Men$SilentGeneration2014+
                               sim_coefs[i,7]*Men$rep+
                               sim_coefs[i,8]*Men$otherparty+
                               sim_coefs[i,9]*Men$voted2012+
                               sim_coefs[i,10]*Men$obamavote2012+
                               sim_coefs[i,11]*Men$totalreceipts+
                               sim_coefs[i,12]*Men$female2*1+
                               sim_coefs[i,13]*Men$Millennials2014*1 +
                               sim_coefs[i,14]*Men$GenerationX2014*1 +
                               sim_coefs[i,15]*Men$SilentGeneration2014*1+
                               sim_coefs[i,16]*Men$rep*1+
                               sim_coefs[i,17]*Men$otherparty*1))
  w0.Women[i] <- mean(invlogit(sim_coefs[i,1] + sim_coefs[i,2]*0 + 
                                 sim_coefs[i,3]*Women$female2 + 
                                 sim_coefs[i,4]*Women$Millennials2014 +
                                 sim_coefs[i,5]*Women$GenerationX2014 +
                                 sim_coefs[i,6]*Women$SilentGeneration2014+
                                 sim_coefs[i,7]*Women$rep+
                                 sim_coefs[i,8]*Women$otherparty+
                                 sim_coefs[i,9]*Women$voted2012+
                                 sim_coefs[i,10]*Women$obamavote2012+
                                 sim_coefs[i,11]*Women$totalreceipts+
                                 sim_coefs[i,12]*Women$female2*0+
                                 sim_coefs[i,13]*Women$Millennials2014*0 +
                                 sim_coefs[i,14]*Women$GenerationX2014*0 +
                                 sim_coefs[i,15]*Women$SilentGeneration2014*0+
                                 sim_coefs[i,16]*Women$rep*0+
                                 sim_coefs[i,17]*Women$otherparty*0))
  w1.Women[i] <- mean(invlogit(sim_coefs[i,1] + sim_coefs[i,2]*1 + 
                                 sim_coefs[i,3]*Women$female2 + 
                                 sim_coefs[i,4]*Women$Millennials2014 +
                                 sim_coefs[i,5]*Women$GenerationX2014 +
                                 sim_coefs[i,6]*Women$SilentGeneration2014+
                                 sim_coefs[i,7]*Women$rep+
                                 sim_coefs[i,8]*Women$otherparty+
                                 sim_coefs[i,9]*Women$voted2012+
                                 sim_coefs[i,10]*Women$obamavote2012+
                                 sim_coefs[i,11]*Women$totalreceipts+
                                 sim_coefs[i,12]*Women$female2*1+
                                 sim_coefs[i,13]*Women$Millennials2014*1 +
                                 sim_coefs[i,14]*Women$GenerationX2014*1 +
                                 sim_coefs[i,15]*Women$SilentGeneration2014*1+
                                 sim_coefs[i,16]*Women$rep*1+
                                 sim_coefs[i,17]*Women$otherparty*1))
}
effect.Men	<- w1.Men - w0.Men 
quantile(effect.Men, c(.025,.975)) #to get CIs; 
mean(effect.Men) ## mean effect 

effect.Women	<-  w1.Women - w0.Women
quantile(effect.Women, c(.025,.975)) #to get CIs; 
mean(effect.Women) 


#calculate and plot results by party.
DEM <- subset(sample, dem==1) 
OP <- subset(sample, otherparty==1) 
REP <- subset(sample, rep==1)

w0.DEM<-NULL
w1.DEM<- NULL
w0.OP<-NULL
w1.OP<-NULL
w0.REP<-NULL
w1.REP<-NULL

for(i in 1:n_draws){ 
  w0.DEM[i] <- mean(invlogit(sim_coefs[i,1] + sim_coefs[i,2]*0 + 
                               sim_coefs[i,3]*DEM$female2 + 
                               sim_coefs[i,4]*DEM$Millennials2014 +
                               sim_coefs[i,5]*DEM$GenerationX2014 +
                               sim_coefs[i,6]*DEM$SilentGeneration2014+
                               sim_coefs[i,7]*DEM$rep+
                               sim_coefs[i,8]*DEM$otherparty+
                               sim_coefs[i,9]*DEM$voted2012+
                               sim_coefs[i,10]*DEM$obamavote2012+
                               sim_coefs[i,11]*DEM$totalreceipts+
                               sim_coefs[i,12]*DEM$female2*0+
                               sim_coefs[i,13]*DEM$Millennials2014*0 +
                               sim_coefs[i,14]*DEM$GenerationX2014*0 +
                               sim_coefs[i,15]*DEM$SilentGeneration2014*0+
                               sim_coefs[i,16]*DEM$rep*0+
                               sim_coefs[i,17]*DEM$otherparty*0))
  w1.DEM[i] <- mean(invlogit(sim_coefs[i,1] + sim_coefs[i,2]*1 + 
                               sim_coefs[i,3]*DEM$female2 + 
                               sim_coefs[i,4]*DEM$Millennials2014 +
                               sim_coefs[i,5]*DEM$GenerationX2014 +
                               sim_coefs[i,6]*DEM$SilentGeneration2014+
                               sim_coefs[i,7]*DEM$rep+
                               sim_coefs[i,8]*DEM$otherparty+
                               sim_coefs[i,9]*DEM$voted2012+
                               sim_coefs[i,10]*DEM$obamavote2012+
                               sim_coefs[i,11]*DEM$totalreceipts+
                               sim_coefs[i,12]*DEM$female2*1+
                               sim_coefs[i,13]*DEM$Millennials2014*1 +
                               sim_coefs[i,14]*DEM$GenerationX2014*1 +
                               sim_coefs[i,15]*DEM$SilentGeneration2014*1+
                               sim_coefs[i,16]*DEM$rep*1+
                               sim_coefs[i,17]*DEM$otherparty*1))
  w0.OP[i] <- mean(invlogit(sim_coefs[i,1] + sim_coefs[i,2]*0 + 
                              sim_coefs[i,3]*OP$female2 + 
                              sim_coefs[i,4]*OP$Millennials2014 +
                              sim_coefs[i,5]*OP$GenerationX2014 +
                              sim_coefs[i,6]*OP$SilentGeneration2014+
                              sim_coefs[i,7]*OP$rep+
                              sim_coefs[i,8]*OP$otherparty+
                              sim_coefs[i,9]*OP$voted2012+
                              sim_coefs[i,10]*OP$obamavote2012+
                              sim_coefs[i,11]*OP$totalreceipts+
                              sim_coefs[i,12]*OP$female2*0+
                              sim_coefs[i,13]*OP$Millennials2014*0 +
                              sim_coefs[i,14]*OP$GenerationX2014*0 +
                              sim_coefs[i,15]*OP$SilentGeneration2014*0+
                              sim_coefs[i,16]*OP$rep*0+
                              sim_coefs[i,17]*OP$otherparty*0))
  w1.OP[i] <- mean(invlogit(sim_coefs[i,1] + sim_coefs[i,2]*1 + 
                              sim_coefs[i,3]*OP$female2 + 
                              sim_coefs[i,4]*OP$Millennials2014 +
                              sim_coefs[i,5]*OP$GenerationX2014 +
                              sim_coefs[i,6]*OP$SilentGeneration2014+
                              sim_coefs[i,7]*OP$rep+
                              sim_coefs[i,8]*OP$otherparty+
                              sim_coefs[i,9]*OP$voted2012+
                              sim_coefs[i,10]*OP$obamavote2012+
                              sim_coefs[i,11]*OP$totalreceipts+
                              sim_coefs[i,12]*OP$female2*1+
                              sim_coefs[i,13]*OP$Millennials2014*1 +
                              sim_coefs[i,14]*OP$GenerationX2014*1 +
                              sim_coefs[i,15]*OP$SilentGeneration2014*1+
                              sim_coefs[i,16]*OP$rep*1+
                              sim_coefs[i,17]*OP$otherparty*1))
  w0.REP[i] <- mean(invlogit(sim_coefs[i,1] + sim_coefs[i,2]*0 + 
                               sim_coefs[i,3]*REP$female2 + 
                               sim_coefs[i,4]*REP$Millennials2014 +
                               sim_coefs[i,5]*REP$GenerationX2014 +
                               sim_coefs[i,6]*REP$SilentGeneration2014+
                               sim_coefs[i,7]*REP$rep+
                               sim_coefs[i,8]*REP$otherparty+
                               sim_coefs[i,9]*REP$voted2012+
                               sim_coefs[i,10]*REP$obamavote2012+
                               sim_coefs[i,11]*REP$totalreceipts+
                               sim_coefs[i,12]*REP$female2*0+
                               sim_coefs[i,13]*REP$Millennials2014*0 +
                               sim_coefs[i,14]*REP$GenerationX2014*0 +
                               sim_coefs[i,15]*REP$SilentGeneration2014*0+
                               sim_coefs[i,16]*REP$rep*0+
                               sim_coefs[i,17]*REP$otherparty*0))
  w1.REP[i] <- mean(invlogit(sim_coefs[i,1] + sim_coefs[i,2]*1 + 
                               sim_coefs[i,3]*REP$female2 + 
                               sim_coefs[i,4]*REP$Millennials2014 +
                               sim_coefs[i,5]*REP$GenerationX2014 +
                               sim_coefs[i,6]*REP$SilentGeneration2014+
                               sim_coefs[i,7]*REP$rep+
                               sim_coefs[i,8]*REP$otherparty+
                               sim_coefs[i,9]*REP$voted2012+
                               sim_coefs[i,10]*REP$obamavote2012+
                               sim_coefs[i,11]*REP$totalreceipts+
                               sim_coefs[i,12]*REP$female2*1+
                               sim_coefs[i,13]*REP$Millennials2014*1 +
                               sim_coefs[i,14]*REP$GenerationX2014*1 +
                               sim_coefs[i,15]*REP$SilentGeneration2014*1+
                               sim_coefs[i,16]*REP$rep*1+
                               sim_coefs[i,17]*REP$otherparty*1))
}
effect.DEM	<- w1.DEM - w0.DEM 
quantile(effect.DEM, c(.025,.975)) #to get CIs; 
mean(effect.DEM) ## mean effect   

effect.OP	<-  w1.OP - w0.OP
quantile(effect.OP, c(.025,.975)) #to get CIs;  
mean(effect.OP) # mean effect  

effect.REP	<-  w1.REP - w0.REP
quantile(effect.REP, c(.025,.975)) #to get CIs; 
mean(effect.REP) # mean effect  


#Step 5.Store the Effects.
Election<-c(rep("2014 General",11))
Effect<-c(meaneffect_m1, mean(effect.Women), mean(effect.Men), mean(effect.DEM), mean(effect.OP), mean(effect.REP), NA,mean(effect.M),mean(effect.GX),mean(effect.BB),mean(effect.SG))
LowerCI<-c(lowerci_m1,  quantile(effect.Women, c(.025)), quantile(effect.Men, c(.025)), quantile(effect.DEM, c(.025)),quantile(effect.OP, c(.025)),quantile(effect.REP, c(.025)),NA, quantile(effect.M, c(.025)),quantile(effect.GX, c(.025)),quantile(effect.BB, c(.025)),quantile(effect.SG, c(.025)))
UpperCI<-c(upperci_m1, quantile(effect.Women, c(.975)), quantile(effect.Men, c(.975)), quantile(effect.DEM, c(.975)),quantile(effect.OP, c(.975)),quantile(effect.REP, c(.975)),NA, quantile(effect.M, c(.975)),quantile(effect.GX, c(.975)),quantile(effect.BB, c(.975)),quantile(effect.SG, c(.975)))
PAgeneral<-data.frame(Effect, UpperCI, LowerCI, Election) 
PAgeneral$`Registrant Subgroup`<-factor(c(1,2,3,4,5,6,7,8,9,10,11), levels=c(1,2,3,4,5,6,7,8,9,10,11),
                                        labels=c("Overall", "Women", "Men", "Democrats","Other\nParty", "Republicans","Gen. Z", "Millennials", "Gen. X", "Baby\nBoomers", "Silent Gen."), ordered = T)
#write effects to data frame.
write.csv(PAgeneral, "PA2014GeneralCompetitivenessControl.csv", row.names=F)
